import { Body, Controller, Post, Inject } from '@midwayjs/core';
import { ApiResponse, ApiTags } from '@midwayjs/swagger';
import { Context } from '@midwayjs/koa';
import { RepairItemService } from '../service/repairItem.service';
import { ServerResponse } from '../util/response';
import {
  RepairItemId,
  RepairItemName,
  FlagResDto,
  RepairItemResDto,
  RepairItem,
} from '../model';
@ApiTags(['维修项'])
@Controller('/repairItem')
export class RepairItemController {
  @Inject()
  ctx: Context;

  @Inject()
  repairItemService: RepairItemService;

  @Post('/list', { summary: '维修项列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: RepairItemResDto,
  })
  async list() {
    const list = await this.repairItemService.getList();
    return ServerResponse.JSON_TPL({
      list,
    });
  }

  @Post('/add', { summary: '添加维修项' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async add(@Body() req: RepairItemName) {
    const res = await this.repairItemService.addItem(req.name);
    if (res[1]) {
      return ServerResponse.JSON_TPL({
        flag: res[1],
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: res[1],
      },
      res[1],
      '该维修项已存在'
    );
  }

  @Post('/edit', { summary: '修改维修项' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async edit(@Body() req: RepairItem) {
    const res = await this.repairItemService.editItem(req.id, req.name);
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在维修项'
    );
  }

  @Post('/del', { summary: '删除维修项' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async del(@Body() req: RepairItemId) {
    const res = await this.repairItemService.delItem(req.id);
    if (Boolean(res)) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res),
      });
    }
    return ServerResponse.JSON_TPL({
      flag: Boolean(res),
    },Boolean(res),'不存在该维修项');
  }
}
